package cn.tedu.baking.controller;

import cn.tedu.baking.aop.RequiredTime;
import cn.tedu.baking.response.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 测试  cookie  和  session 的使用
 */
@RestController
@RequestMapping("/test/")
public class TestController {
  /*  //  localhost:8080/test/cookie01
    @GetMapping("cookie01")
    public JsonResult cookie01(HttpServletResponse response){
        Cookie username = new Cookie("username","keji");
        Cookie password = new Cookie("password","123456");
        //将创建的cookie 信息 响应给浏览器
        response.addCookie(username);
        response.addCookie(password);
        return JsonResult.ok();
    }
    @GetMapping("cookie02")
    public JsonResult cookie02(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            System.out.println("键为:"+cookies[i].getName());
            System.out.println("值为:"+cookies[i].getValue());
        }
        return JsonResult.ok();
    }
    //  localhost:8080/test/session01

    @GetMapping("session01")
    public JsonResult session01(HttpSession session){
        session.setAttribute("username","zhangsan");
        session.setAttribute("password","123456");
        return JsonResult.ok();
    }

    @GetMapping("session02")
    public JsonResult session02(HttpSession session){
        String username = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");
        System.out.println("用户是:"+username + "密码是:"+password);
        return JsonResult.ok();
    }
*/
   //基于目前的需求: 如果我想要给BannerController这个控制器类中的所有方法添加耗时计算的逻辑
   // 最好不要修改原来的代码!

    @GetMapping("wan1")
    //贴个"便利贴"(注解)
    public JsonResult wan1(){
        for (int i = 1; i <= 1000; i++) {
            System.out.println("打印第:" + i +"次" );
        }
        return JsonResult.ok();
    }

    @GetMapping("wan2")
    @RequiredTime
    public JsonResult wan2(){
        for (int i = 1; i <= 10000; i++) {
            System.out.println("打印第:" + i +"次" );
        }
        return JsonResult.ok();
    }
    @GetMapping("wan3")
    public JsonResult wan3(){
        int i = 1/0;
        System.out.println("wan3方法执行了!!");

        return JsonResult.ok();
    }

}
